/**
 *	@project:	kybernetes
 *	@author:	D.Toepel
 *	@note:		---
 *	
 *	Copyright (C) 2009  D.Toepel <de.dtools> {at} <gmail> {dot} <com>
 *	
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 */
package dt.htwk.extras;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Hashtable;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.border.TitledBorder;

import dt.htwk.res.Resource;


/**
 * <h3>Template</h3>
 *
 * @author DToepel
 *
 */
public class TestServer extends JFrame {
	
/* ****************************************************
 * Deklaration der Variablen und Komponenten
 * ***************************************************/

	/**
	 * serialVersionUID -- Standard: 1L
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 
	 */
	private static Hashtable<String,String> lng;

	private static JTextArea ta;

	private JTextField port;
	
	private static TestServerThread tsThread = new TestServerThread();
	

/* ****************************************************
 * Aufruf des Konstruktors
 * ***************************************************/

	public TestServer(){
		super();
		initGUI();
	}
	
/* ****************************************************
 * PUBLIC - oeffentliche Methoden
 * ***************************************************/

	/**
	 * <h3>setLanguageContent</h3>
	 *
	 * @param lng
	 */
	public static void setLanguageContent(Hashtable<String,String> lng){
		TestServer.lng = lng;
	}
	

/* ****************************************************
 * PRIVATE - nicht-oeffentliche Methoden
 * ***************************************************/

	private void initGUI(){
		InetAddress thisIp = null;
		String ip = null;
		try {
			thisIp = InetAddress.getLocalHost();
			ip =  thisIp.getHostAddress();
		} catch (UnknownHostException e) {}
		
		this.setSize(600, 400);
		this.setLayout(new BorderLayout());
		this.setTitle(lng.get("ts-title") + " - IP: " + ip);
		this.add(getHeader(), BorderLayout.PAGE_START);
		this.add(getOutput(), BorderLayout.CENTER);
	}

	/**
	 * <h3>getTitledPanel</h3>
	 *
	 * @param titel
	 * @return
	 */
	private JPanel getTitledPanel(String titel, LayoutManager layout){
		JPanel labelPanel = new JPanel(layout);
		labelPanel.setOpaque(false);
		labelPanel.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(new java.awt.Color(110,110,110)), 
				" "+ titel + " ", TitledBorder.LEFT , TitledBorder.TOP,
				new java.awt.Font("Verdana",1,12), Resource.darkMarineBlue));
		labelPanel.setPreferredSize(new Dimension(100,20));
		return labelPanel;
	}
	
	/**
	 * <h3>getHeader</h3>
	 *
	 * @return
	 */
	private JPanel getHeader() {
		JPanel header = getTitledPanel(lng.get("ts-header"), new BorderLayout());
		header.setPreferredSize(new Dimension(Integer.MAX_VALUE, 80));
		header.setLayout(new BoxLayout(header, BoxLayout.LINE_AXIS));
		{
			JLabel portLabel = new JLabel(lng.get("ts-port"));
			portLabel.setBorder(BorderFactory.createEmptyBorder(0,5,0,10));
			header.add(portLabel);
			
			port = new JTextField("18098");
			port.setPreferredSize(new Dimension(150, 30));
			port.setMaximumSize(new Dimension(150, 30));
			header.add(port);
			
			portLabel.setLabelFor(port);
			
			header.add(Box.createHorizontalGlue());
			
			final JToggleButton btnStart = new JToggleButton(lng.get("ts-button1-1"));
			btnStart.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if (btnStart.isSelected()){
						btnStart.setText(lng.get("ts-button1-2"));
						tsThread.setOutput(ta);
						tsThread.setPort(Integer.parseInt(port.getText()));
						tsThread.start();
					}else{
						btnStart.setText(lng.get("ts-button1-1"));
						tsThread.stopThread();
					}
				}
			});
			header.add(btnStart);
			
			JButton btnClear = new JButton(lng.get("ts-button2"));
			btnClear.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					ta.setText("");
				}
			});
			header.add(btnClear);
		}
		return header;
	}

	/**
	 * <h3>getOutput</h3>
	 *
	 * @return
	 */
	private JPanel getOutput() {
		JPanel output = getTitledPanel(lng.get("ts-output"), new BorderLayout());
		ta = new JTextArea();
		ta.setEditable(false);
		output.add(new JScrollPane(ta), BorderLayout.CENTER);
		return output;
	}

}
